define(['angularAMD'], function (angularAMD) {
    angularAMD.service('G0102Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            this.init = function(options){
                $mdDialog.dialog(
                    'module/moduleG/program/G01/G0102/G0102.html',
                    function(dialog){
                        return {
                            css_url: 'module/moduleG/program/G01/G0102/G0102.css',
                            parent: options.parent,
                            subOpCondition: options.subOpCondition,
                            beforeShown: function(){
                            },
                            loadOpNoList: function(){
                                $rootScope.showQueryList({
                                    pageRecord: 25,
                                    header: [
                                        {label:$filter('translate')('common.queryList.op_c'), code:'page_no'},
                                        {label:$filter('translate')('common.queryList.op_n'), code:'page_name'}
                                    ],
                                    query: function(conditions, feedback){
                                        var content = {
                                            page_mode: 38,
                                            query_condition_info: []
                                        };
                                        if(config.setting.OperatingArea != ''){
                                            content.query_condition_info.push({column_name:'MASTERNO', query_mode:'0', value: config.setting.OperatingArea, merge_condition_model:'AND' });
                                        }
                                        //20180525 modify by 李栋 for mantis#0047129
                                        $MMWService.pageNumberInfoGet(content, function(data){
                                                var temp = $filter('filter')(data, {favourite:'1'});
                                                //var temp = data.page_number_info_get;
                                                if(temp && temp.length){
                                                    feedback(temp);
                                                } else {
                                                    $rootScope.showConfirm($filter('translate')('G0102.msg.favorite_empty'), function(confirmDialog){
                                                        confirmDialog.hide();
                                                        $timeout(function(){
                                                            $scope.favorite(function(){
                                                                $scope.loadOpNoList();
                                                            });
                                                        }, 200);
                                                    });
                                                }
                                            }
                                        );
                                    },
                                    selectCode: dialog.subOpCondition.op_no||'',
                                    confirm : function(selectItem, dialogType){
                                        dialog.subOpCondition.op_no = selectItem.page_no;
                                        dialog.subOpCondition.op_name = selectItem.page_name;
                                        dialogType.hide();
                                    }
                                });
                            },
                            loadPlotNoList: function(){
                                if (dialog.subOpCondition.op_no != '') {
                                    var _options = {
                                        page_mode: '',
                                        condition: [],
                                        pageRecord: 25,
                                        header: [
                                            {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'},
                                            {label:$filter('translate')('G0102.queryList.cur_qty'), code:'cur_qty'}
                                        ],
                                        selectCode: dialog.subOpCondition.plot_no,
                                        setCondition:function(conditions){
                                            if(dialog.subOpCondition.op_no != '')
                                                conditions.push({column_name:'OPNo', query_mode:'2', value: dialog.subOpCondition.op_no });
                                            return conditions;
                                        },
                                        confirm : function(selectItem){
                                            dialog.subOpCondition.plot_no = selectItem.page_no;
                                            dialog.confirm();
                                        }
                                    };
                                    if(dialog.subOpCondition.is_check_in){
                                        _options.page_mode = '36-1';
                                        _options.condition.push({name:$filter('translate')('common.queryList.plot_no'), column_name:'LotNo', query_mode:'2', value: dialog.subOpCondition.plot_no});
                                    } else {
                                        _options.page_mode = '36-2';
                                        _options.condition.push({name:$filter('translate')('common.queryList.plot_no'), column_name:'ds.LotNo', query_mode:'2', value: dialog.subOpCondition.plot_no});
                                    }
                                    $MMWService.showQueryListPNIG(_options);
                                }
                                else {
                                    $rootScope.showAlert($filter('translate')('G0102.msg.op_no_empty'));
                                }
                            },
                            back: function () {
                                dialog.hide();
                                if(options.close)
                                    options.close();
                            },
                            confirm: function () {
                                if(options.callback)
                                    options.callback(dialog);
                            }
                        };
                    }
                );
            };
        }]);
});
